1 using System;
2 using UnityEngine;
3 using Random = UnityEngine.Random;
4
5 namespace UnityStandardAssets.Vehicles.Aeroplane
6 {
7 [RequireComponent(typeof (AeroplaneController))]
8 public class AeroplaneAiControl : MonoBehaviour
9 {
10 // This script represents an AI 'pilot' capable of flying the plane towards a designated target.
11 // It sends the equivalent of the inputs that a user would send to the Aeroplane controller.
12 [SerializeField] private float m_RollSensitivity = .2f; // How sensitively the AI applies the roll controls
13 [SerializeField] private float m_PitchSensitivity = .5f; // How sensitively the AI applies the pitch controls
14 [SerializeField] private float m_LateralWanderDistance = 5; // The amount that the plane can wander by when heading for a target
15 [SerializeField] private float m_LateralWanderSpeed = 0.11f; // The speed at which the plane will wander laterally
16 [SerializeField] private float m_MaxClimbAngle = 45; // The maximum angle that the AI will attempt to make plane can climb at
17 [SerializeField] private float m_MaxRollAngle = 45; // The maximum angle that the AI will attempt to u
18 [SerializeField] private float m_SpeedEffect = 0.01f; // This increases the effect of the controls based on the plane's speed.
19 [SerializeField] private float m_TakeoffHeight = 20; // the AI will fly straight and only pitch upwards until reaching this height
20 [SerializeField] private Transform m_Target; // the target to fly towards
21
22 private AeroplaneController m_AeroplaneController; // The aeroplane controller that is used to move the plane
23 private float m_RandomPerlin; // Used for generating random point on perlin noise so that the plane will wander off path slightly
24 private bool m_TakenOff; // Has the plane taken off yet
25
26
27 // setup script properties
28 private void Awake()
29 {
30 // get the reference to the aeroplane controller, so we can send move input to it and read its current state.
31 m_AeroplaneController = GetComponent<AeroplaneController>();
32
33 // pick a random perlin starting point for lateral wandering
34 m_RandomPerlin = Random.Range(0f, 100f);
35 }
36
37
38 // reset the object to sensible values
39 public void Reset()
40 {
41 m_TakenOff = false;
42 }
43
44
45 // fixed update is called in time with the physics system update
46 private void FixedUpdate()
47 {
48 if (m_Target != null)
49 {
50 // make the plane wander from the path, useful for making the AI seem more human, less robotic.
51 Vector3 targetPos = m_Target.position +
52 transform.right*
53 (Mathf.PerlinNoise(Time.time*m_LateralWanderSpeed, m_RandomPerlin)*2 - 1)*
54 m_LateralWanderDistance;
55
56 // adjust the yaw and pitch towards the target
57 Vector3 localTarget = transform.InverseTransformPoint(targetPos);
58 float targetAngleYaw = Mathf.Atan2(localTarget.x, localTarget.z);
59 float targetAnglePitch = -Mathf.Atan2(localTarget.y, localTarget.z);
60
61
62 // Set the target for the planes pitch, we check later that this has not passed the maximum threshold
63 targetAnglePitch = Mathf.Clamp(targetAnglePitch, -m_MaxClimbAngle*Mathf.Deg2Rad,
64 m_MaxClimbAngle*Mathf.Deg2Rad);
65
66 // calculate the difference between current pitch and desired pitch
67 float changePitch = targetAnglePitch - m_AeroplaneController.PitchAngle;
68
69 // AI always applies gentle forward throttle
70 const float throttleInput = 0.5f;
71
72 // AI applies elevator control (pitch, rotation around x) to reach the target angle
73 float pitchInput = changePitch*m_PitchSensitivity;
74
75 // clamp the planes roll
76 float desiredRoll = Mathf.Clamp(targetAngleYaw, -m_MaxRollAngle*Mathf.Deg2Rad, m_MaxRollAngle*Mathf.Deg2Rad);
77 float yawInput = 0;
78 float rollInput = 0;
79 if (!m_TakenOff)
80 {
81 // If the planes altitude is above m_TakeoffHeight we class this as taken off
82 if (m_AeroplaneController.Altitude > m_TakeoffHeight)
83 {
84 m_TakenOff = true;
85 }
86 }
87 else
88 {
89 // now we have taken off to a safe height, we can use the rudder and ailerons to yaw and roll
90 yawInput = targetAngleYaw;
91 rollInput = -(m_AeroplaneController.RollAngle - desiredRoll)*m_RollSensitivity;
92 }
93
94 // adjust how fast the AI is changing the controls based on the speed. Faster speed = faster on the controls.
95 float currentSpeedEffect = 1 + (m_AeroplaneController.ForwardSpeed*m_SpeedEffect);
96 rollInput *= currentSpeedEffect;
97 pitchInput *= currentSpeedEffect;
98 yawInput *= currentSpeedEffect;
99
100 // pass the current input to the plane (false = because AI never uses air brakes!)
101 m_AeroplaneController.Move(rollInput, pitchInput, yawInput, throttleInput, false);
102 }
103 else
104 {
105 // no target set, send zeroed input to the planeW
106 m_AeroplaneController.Move(0, 0, 0, 0, false);
107 }
108 }
109
110
111 // allows other scripts to set the plane's target
112 public void SetTarget(Transform target)
113 {
114 m_Target = target;
115 }
116 }
117 }